﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using LGSVR.Common;

namespace LGSVR_WEB.sys
{
    public partial class MenuInfoList : LGSVR.Common.DataBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserID"] == null)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "out", "<script>alert('系统超时或服务器故障，请退出重新登录！');window.parent.parent.navigate('../Logout.aspx');</script>");
            }
            if (!IsPostBack)
            {
                innit();
                gvDataBind("");
            }
         
        }

        private void innit()
        {
           
            lbtnDel.Attributes.Add("onclick", "return checkDel('ListInfo')");
        }

        private void gvDataBind(string p)
        {
            DataTable dt;
            int totalRS;
            int pageIndex;


            if (!string.IsNullOrEmpty(Request.QueryString["pg"]) && Utils.IsNumber(Request.QueryString["pg"]))
            {
                pageIndex = int.Parse(Request.QueryString["pg"]);
            }
            else
            {
                pageIndex = 1;
            }

            dt = GetList("menuInfo", " id desc ", p, pageIndex, PageSize, out totalRS);
            rptBody.DataSource = dt;
            rptBody.DataBind();
            SplitHtmlCode = Split.GetHtmlCode("MenuInfoList.aspx", 8, pageIndex, PageSize, totalRS);

        }

        protected void Search(object sender, CommandEventArgs e)
        {
            string strWhere = "";
            if (txtmenuName.Text.Trim() != "")
            {
                strWhere = " and menuName like '%" + txtmenuName.Text.Trim() + "%'";
            }
            
            gvDataBind(strWhere);

        }

        protected void lbtnDel_Click(object sender, EventArgs e)
        {

            //bool isTrue = Delete("menuInfo", Request["chkId"]);
            try
            {
               
                    int totalRS;
                    DataTable dt = GetList("SELECT ActMenu.ID AS AMID,menuInfo.ID FROM menuInfo,ActMenu WHERE menuInfo.menuID = ActMenu.menuID", " ID desc ", " AND ID=" + Request["chkId"], 1, 10, out totalRS);
                    string strID = string.Empty;
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        foreach (DataRow dr in dt.Rows)
                        {
                            strID += dr["AMID"].ToString() +",";
                        }

                    }

                    if (Delete("ActMenu", strID.TrimEnd(',')))
                    {
                        if (Delete("menuInfo", Request["chkId"]))
                        {
                            MsgContent = ShowMessage("恭喜您，删除成功！", MessageType.Success, false, true);
                            NoRefresh();
                        }
                    }
                   
                
            }
            catch
            {
                MsgContent = ShowMessage("对不起,操作失败，请重试。", MessageType.Error, false, true);

            }
            //if (isTrue)
            //{
            //    MsgContent = ShowMessage("恭喜您，删除成功！", MessageType.Success, false, true);
            //    NoRefresh();
            //}
            //else
            //{
            //    MsgContent = ShowMessage("对不起,操作失败，请重试。", MessageType.Error, false, true);
            //}
        }

      
    }
}